package com.cysion.study.ui.fragment

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.cysion.other.color
import com.cysion.other.startActivity_ex
import com.cysion.study.R
import com.cysion.study.adapter.POrderChildFragmentAdapter
import com.cysion.study.entity.POrderListEntity
import com.cysion.study.helper.UserCache
import com.cysion.study.ktbox.Box
import com.cysion.study.ktbox.base.BaseFragment
import com.cysion.study.ktbox.base.ITEM_CLICK
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.net.ErrorHandler
import com.cysion.study.ktbox.utils.darkTextTheme
import com.cysion.study.net.ApiClient
import com.cysion.study.ui.NoticeActivity
import com.cysion.study.ui.POrderDetailActivity
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.widget.ShowToast
import com.cysion.study.widget.UIHelper
import com.cysion.targetfun._subscribe
import com.scwang.smartrefresh.layout.constant.RefreshState
import kotlinx.android.synthetic.main.fragment_order_child.*

/**
 * 家教新首页
 * Created by chu on 2019-06-30.
 */
class POrderChildFragment:BaseFragment(), View.OnClickListener {
    override fun getLayoutId(): Int {
        return R.layout.fragment_order_child
    }

    private var type=""
    override fun initView() {
        activity!!.darkTextTheme(activity!!.color(R.color.white))
        type = arguments?.getString("type").toString()
        initRecyclerView()
        initRefreshLayout()

    }

    private lateinit var mAdapter: POrderChildFragmentAdapter
    private val dataList: MutableList<POrderListEntity> = mutableListOf()
    private var curPage = 1

    override fun initData() {
        super.initData()
        smartLayout.autoRefresh()
    }
    //    初始化刷新控件
    private fun initRefreshLayout() {
        smartLayout.setOnRefreshListener {
            curPage = 1
            getDataList()
            smartLayout.setEnableLoadMore(true)

        }
        smartLayout.setOnLoadMoreListener {
            getDataList()
        }
    }

    //初始化列表
    private fun initRecyclerView() {
        rv_list.isNestedScrollingEnabled = false
        mAdapter = POrderChildFragmentAdapter(dataList, Box.context)
        rv_list.adapter = mAdapter
        rv_list.layoutManager = LinearLayoutManager(Box.context)
        mAdapter.setOnTypeClickListener { obj, position, flag ->
            if (flag == ITEM_CLICK) {//
                var bundle = Bundle()
                bundle.putString("orderId",obj.id)//
                bundle.putString("byType","2")//byType-1根据订单号请求订单详情，2订单id请求订单详情
                UIHelper.jump(activity, POrderDetailActivity::class.java,bundle)//订单详情
            }
        }
        //模拟数据
        /*if (mAdapter.itemCount == 0) {
            var lists: MutableList<String> = mutableListOf("","")
            dataList.addAll(lists)
            mAdapter.notifyDataSetChanged()
        }*/
    }
    private fun getDataList() {
//        Alert.loading(context)
        stopRefresh()
        ApiClient.api.getOrderList(UserCache.token,type,curPage.toString(),"10").compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
//                    Alert.close()
                    stopRefresh()
                    if (curPage == 1) {
                        dataList.clear()
                        if (it.size == 0) {
                            multiView.showEmpty()
                        }
                    }
                    if (it.size <= 0) {
                        smartLayout.setEnableLoadMore(false)
                    } else {
                        curPage++
                        dataList.addAll(it)
                        mAdapter.notifyDataSetChanged()
                        multiView.showContent()
                    }
                }
                _onError {
                    Alert.close()
                    ShowToast(ErrorHandler.handle(it).errorMsg)
                    stopRefresh()
                }
            }
    }

    private fun stopRefresh() {
        if (smartLayout.state == RefreshState.Refreshing) {
            smartLayout.finishRefresh()
        } else if (smartLayout.state == RefreshState.Loading) {
            smartLayout.finishLoadMore(100)
        }
    }


    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_notice -> {//通知列表

                context.startActivity_ex<NoticeActivity>()
            }
            R.id.tv_attent ->{//教师租赁
//                context.startActivity_ex<AttendenceActivity>()
            }
            R.id.tv_appro ->{//招聘
//                context.startActivity_ex<MyApprovalActivity>()

            }
            R.id.tv_source_more ->{//查看所有日程
//                context.startActivity_ex<CourseMoreActivity>()旧课表

            }
            R.id.tv_perfect ->{
                ShowToast("完善")
            }

        }
    }


    override fun closeMvp() {
    }
}